package ccxz.biz.controller;

import ccxz.biz.SetupApplication;
import ccxz.biz.service.UserService;
import ccxz.biz.view.IndexView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @Author CZM
 * @create 2020/6/25 17:28
 */
@FXMLController
public class LoginController implements Initializable {


    @FXML
    private TextField loginName;

    @FXML
    private PasswordField loginPassword;

//    @FXML
//    private TextField registerName;
//
//    @FXML
//    private PasswordField registerPassword;


    @FXML
    private Button loginButton;
    @Autowired
    UserService userService;

    /**
     * 登陆
     */
    @FXML
    public void login() {
        System.out.println(loginName.getText()+":"+loginPassword.getText());
        String name = loginName.getText();
        String password = loginPassword.getText();
        if (name.equals("") || password.equals("")){
            Alert a = new Alert(Alert.AlertType.ERROR);
            a.initOwner(SetupApplication.getStage());
            a.setHeaderText("用户名、密码必填！");
            a.showAndWait();
        }else if (userService.login(name,password)){
            SetupApplication.getStage().close();
            if (name.equals("root")) {
                SetupApplication.ROOT = true;
            }else {
                SetupApplication.ROOT = false;
            }
            SetupApplication.showView(IndexView.class);
        }else {
            Alert a = new Alert(Alert.AlertType.ERROR);
            a.initOwner(SetupApplication.getStage());
            a.setHeaderText("用户名或密码错误！");
            a.showAndWait();
            loginName.clear();
            loginPassword.clear();
        }

    }



    @Override
    public void initialize(URL location, ResourceBundle resources) {
        SetupApplication.controllers.put(this.getClass().getSimpleName(), this);
    }

}
